package com.sdy.hospitalguard.h5.controller;

import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.common.utils.Assert;
import com.sdy.hospitalguard.biz.dto.QrcodeDataDTO;
import com.sdy.hospitalguard.biz.model.ApplyPost;
import com.sdy.hospitalguard.biz.service.ApplyPostService;
import com.sdy.hospitalguard.biz.service.QrcodeService;
import com.sdy.mvc.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author cgj
 * @since 2020/05/09
 */
@RestController
@RequestMapping("/qrcode")
@Api(tags = "二维码相关接口")
@Slf4j
public class QrcodeController extends BaseController {
    @Autowired
    private QrcodeService qrcodeService;
    @Autowired
    private ApplyPostService applyPostService;

    @PostMapping("/createQRcode")
    @ApiOperation(value = "生成通行码", response = ApplyPost.class)
    public Response createQRcode(HttpServletRequest request) throws BizException {
        Integer userId = getUserId(request);
        Assert.isNull(userId, "登陆信息未生效!");
        Boolean aBoolean = applyPostService.createQRcode(userId);
        return Response.success(aBoolean);
    }

    @GetMapping("/getQrcodeData")
    @ApiOperation(value = "获取二维码数据", response = QrcodeDataDTO.class)
    public Response<QrcodeDataDTO> getQrcodeData(HttpServletRequest request) throws BizException {
        Integer userId = getUserId(request);
        Assert.isNull(userId, "用户未登录！");
        QrcodeDataDTO qrcodeData = qrcodeService.getQrcodeData(userId);
        return Response.success(qrcodeData);
    }

}
